package qc.module.sl.api;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.client.RestTemplate;
import qc.module.platform.dto.division.AdminDivisionInfoDto;
import qc.module.platform.dto.division.AdminDivisionQueryConditionDto;

import java.util.HashMap;
import java.util.Map;

/**
 * PlatformServiceCall 调用platform微服务
 *
 * @author QuCheng Tech
 * @create 2023/02/20
 */
public class ServiceCall {
    /**
     * 调用/division/query接口获取所有行政区划名称
     * 
     * @param	restTemplate	
     * @param	discoveryClient	
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @author QcCheng Tech
     * @since 2023/2/20
     */
    public Map<String, String> getAdminDivision(RestTemplate restTemplate, DiscoveryClient discoveryClient){
        //从nacos中获取服务地址,微服务远程调用
        ServiceInstance service = discoveryClient.getInstances("module-platform").get(0);

        //获取查询行政区划信息接口地址
        String url = "http://" + service.getHost() + ":" + service.getPort() + "/division/query";

        ///division/query接口所需参数，置为空，查询全部行政区划信息
        AdminDivisionQueryConditionDto divisionDto = new AdminDivisionQueryConditionDto();
        divisionDto.setGeos(false);

        //远程调用获取结果
        AdminDivisionInfoDto[] result = restTemplate.postForObject(url, divisionDto, AdminDivisionInfoDto[].class);

        Map<String, String> adminDivisionMap = new HashMap<>();

        //将行政区划信息存入map对象中
        for (int i = 0; i < result.length; i++) {
            adminDivisionMap.put(result[i].getCode(), result[i].getName());
        }
        
        return adminDivisionMap;
    }
}
